package crl.menu.data
{
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.Dictionary;
	
	public class Config
	{
		private var window:String="_self";
		
		public var ui:String;
		
		private var list:Array;
		
		public var mainURLS:Vector.<Item>;
		public function Config()
		{
			list=[];
			mainURLS=new Vector.<Item>;
		}
		
		public static var instance:Config=new Config();
		
		public function init(xml:XML):void{
			
			var target:String=xml.@window;
			if(target){
				window=target;
			}
			
			ui=xml.@ui;
			
			var xmlList:XMLList=xml.descendants("menu");
			
			var index:int=0;
			var item:Item;
			for each(xml in xmlList){
				item=new Item();
				item.url=xml.child("url")[0].toString();
				item.label=xml.@label;
				mainURLS.push(item);
				
				list[index]=buildSub(xml.descendants("item"));
				
				index++;
			}
			
		}
		
		
		private function buildSub(xmlList:XMLList):Array{
			var result:Array=[];
			for each(var xml:XML in xmlList){
				result.push(new Item().bind(xml));
			}
			return result;
		}
		
		
		public function getList(index:int):Array{
			return list[index];
		}
		
		
		public function toMain(index:int):void{
			
			var item:Item=mainURLS[index];
			toURL(item.url,window);
		}
		
		public function goto(item:Item):void{
			toURL(item.url,item.target)
		}
		
		
		public function toURL(value:String,target:String=""):void{
			
			if(!target){
				target=window;
			}
			
			var bool:Boolean=false;
			if(ExternalInterface.available){
				bool=ExternalInterface.call("toURL", value, target);
			}
			
			//trace(value);
			//return ;
			if(bool==false){
				getURL(value,target);
			}
		}
		
		
		
		public static function getURL(url:String, windows:String = "_blank", parm:String = "") : void
		{
			if (url==null)
			{
				return;
			}
			
			var request:URLRequest = new URLRequest(url);
			if (!ExternalInterface.available)
			{
				navigateToURL(request, windows);
				return;
			}
			
			var browserName:String = getBrowserName();
			if ( browserName== "Firefox" || browserName == "IE")
			{
				var jsMethod:String = "window.open";
				ExternalInterface.call(jsMethod, url, windows, parm);
			}
			else if (browserName == "Safari" || browserName == "Opera")
			{
				navigateToURL(request, windows);
			}
			else
			{
				navigateToURL(request, windows);
			}
		}
		
		
		private static function getBrowserName() : String
		{
			var result:String = null;
			var userAgent:* = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
			
			
			if(userAgent == null){
				return result;
			}
			
			if (userAgent.indexOf("Firefox") >= 0)
			{
				result = "Firefox";
			}
			else if (userAgent.indexOf("Safari") >= 0)
			{
				result = "Safari";
			}
			else if (userAgent.indexOf("MSIE") >= 0)
			{
				result = "IE";
			}
			else if (userAgent.indexOf("Opera") >= 0)
			{
				result = "Opera";
			}
			return result;
		}
		
	}
}